//
//  TradeTools.m
//  EBOexchange
//
//  Created by WWJ on 2017/12/27.
//  Copyright © 2017年 ebo. All rights reserved.
//

#import "WJTools.h"
@implementation WJTools
+ (void)drawDottedLineInView:(UIView *)view dottedLineWidth:(CGFloat)dottedLineWidth lineGap:(int)gap lineHeight:(CGFloat)lineHeight startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint lineColor:(UIColor *)lineColor{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:view.bounds];
    [shapeLayer setPosition:view.center];
    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
     
    // 设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:[lineColor CGColor]];
    
    // 3.0f设置虚线的高度
    [shapeLayer setLineWidth:lineHeight];
    [shapeLayer setLineJoin:kCALineJoinRound];
    
    // 3=线的宽度 1=每条线的间距
    [shapeLayer setLineDashPattern:
     [NSArray arrayWithObjects:[NSNumber numberWithInt:dottedLineWidth],
      [NSNumber numberWithInt:gap],nil]];
    
    // Setup the path
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
    CGPathAddLineToPoint(path, NULL, endPoint.x,endPoint.y);
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    
    [[view layer] addSublayer:shapeLayer];
}
+ (BOOL)isBlankString:(NSString *)string {
    if (string == nil || string == NULL) {
        return YES; }
    if([string isEqualToString:@"(null)"]) {return YES;}
    if ([string isKindOfClass:[NSNull class]]) { return YES; }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
        
    }
    return NO;
}
+ (NSString *)string:(double)doub fractionDigits:(int)num{
    //动态保留几位小数
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    //    formatter.groupingSize = 3;
    //设置最多保留几位小数
    formatter.maximumFractionDigits = num;
    //设置最少保留几位小数
    formatter.minimumFractionDigits = num;
    //由于最多与最少是不矛盾的所以要精确到几位小数只要同时设置就好了，如果只是最多，或只是最少的话可以把上面两行中的一行注释即可
//    formatter.groupingSeparator = @",";
    NSMutableString *str = [NSMutableString stringWithString:[formatter stringFromNumber:@(doub)]];
    
    return [str stringByReplacingOccurrencesOfString:@"," withString:@""];
}

+ (NSString *)dateFromString:(NSString *)dateSring Format:(NSString *)format{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateSring doubleValue]];
    NSDateFormatter *formatter = [NSDateFormatter new];
    formatter.dateFormat = format?format:@"HH:mm";
    NSString *dateStr = [formatter stringFromDate:date];
    return dateStr;
}

//二维码
+ (UIImage *)createNonInterpolatedUIImageWithDataString:(NSString *)dataString withSize:(CGFloat) size
{    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2.恢复默认
    [filter setDefaults];
    // 3.给过滤器添加数据(正则表达式/账号和密码)
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKeyPath:@"inputMessage"];
    CIImage *image = [filter outputImage];
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
    
}

+ (CGSize) boundingRectWithSize:(NSString*) txt Font:(UIFont*) font Size:(CGSize) size
{
    
    CGSize _size;
    
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
    
    NSDictionary *attribute = @{NSFontAttributeName: font};
    
    NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine |
    
    NSStringDrawingUsesLineFragmentOrigin |
    
    NSStringDrawingUsesFontLeading;
    
    _size = [txt boundingRectWithSize:size options: options attributes:attribute context:nil].size;
    
#else
    
    _size = [txt sizeWithFont:font constrainedToSize:size];
    
#endif
    
    return _size;
    
}

+ (NSMutableAttributedString*)changeLabelWithText:(NSString*)needText withChangeText:(NSString *)text withFont:(UIFont *)font  color:(UIColor *)color {
    
    
    NSRange range = [needText rangeOfString:text];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:needText];
    //颜色
    [string addAttribute:NSForegroundColorAttributeName value:color range:range];
    //字体
    [string addAttribute:NSFontAttributeName value:font range:range];
    return string;
}
+ (BOOL)isEnglishWithNum:(NSString *)string {
    
    NSString *regex = @"^[A-Za-z0-9]+$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isValid = [predicate evaluateWithObject:string];
    return isValid;
}
@end
